function countSort (arr) {
  let countArrayLen = Math.max(...arr) + 1
  let countArray = new Array(countArrayLen).fill(0)
  // 把原数组中的num作为计数数组的索引值, 记录下来
  for (let num of arr) {
    countArray[num]++
  }
  let result = []
  for (let i = 0; i < countArray.length; i++) {
    while (countArray[i] > 0) {
      result.push(i)
      countArray[i]--
    }
  }
  return result
}
let arr = [2, 5, 3, 7, 6, 4, 21, 13, 1]
console.log(countSort(arr))